﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Entity;
using MockPrjTeam2.Biz;
namespace MockPrjTeam2.Pages.Forms
{
    public partial class AddDirectorate : System.Web.UI.Page
    {
        private int OrgId;
      
  
        protected void Page_Load(object sender, EventArgs e)
        {
                if (Session["contact"] != null || Session["Address"] != null || Session["Bussname"] != null || Session["Sic"] != null)
            {

                var o = Session["contact"];
                if (o != null) txtContact.Text = o.ToString();
                var a = Session["Address"];
                if (a != null) txtPostCode.Text = a.ToString();
                var b = Session["Bussname"];
                if (b != null) txtBussinessName.Text = b.ToString();
                var s = Session["Sic"];
            }
            string script =
               @"<script>
              function OpenPopupBussiness() {
                popup('../Views/PopupSearchBussiness.aspx');
            }
                 function OpenPopup() {
                popup('PopupListContacts.aspx');
            }
              function OpenPopupAddress() {
                popup('../Views/PopupSearchAddress.aspx');
            }
            function popup(url) {

                var width = 500;
                var height = 400;
                var left = (screen.width - width) / 2;
                var top = (screen.height - height) / 2;
                var params = 'width=' + width + ', height=' + height;
                params += ', top=' + top + ', left=' + left;
                params += ', directories=no';
                params += ', location=no';
                params += ', menubar=no';
                params += ', resizable=no';
                params += ', scrollbars=no';
                params += ', status=no';
                params += ', toolbar=no';
                newwin = window.open(url, 'windowname5', params);
                if (window.focus) { newwin.focus() }
                return false;
            }
            </script>";
            if (!Page.ClientScript.IsStartupScriptRegistered("Mast"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
                                                        "Mast", script);
            }
            if (!IsPostBack)
            {

                GetValue();
                GetCountry();
            }
        }
        private void GetCountry()
        {
            //ddl
            //OrganizationBus bus = new OrganizationBus();
            var bus = new CountryBiz();
            DropDownListNationCounty.DataSource = bus.GetAllCountry();
            DropDownListNationCounty.DataTextField = "CountryName";
            DropDownListNationCounty.DataValueField = "CountryID";
            DropDownListNationCounty.DataBind();

        }
        private void GetValue()
        {
           OrganizationBuz  organizationBuz=new OrganizationBuz();
            if (int.TryParse(Request.Params["organisationId"], out OrgId))
            {
                Organization organization = organizationBuz.GetOrganizationById(OrgId);
                txtBussinessName.Text = organization.BussinessType;
                TxtSicCode.Text = organization.SicCode;
                TextBoxWebAddress.Text = organization.WebAddress;
              
                 HiddenFielAddress.Value= organization.AddressLine1;
                HdnAddressLine2.Value = organization.AddressLine2;
                HdnAddressLine3.Value = organization.AddressLine3;
                hdnBussId.Value =Convert.ToString(organization.BusinessTypeId);
            }
        }

        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            if (int.TryParse(Request.Params["organisationId"], out OrgId))
            {
                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                "OrganizationDetails.aspx?id={0}#DetailsPanel06", OrgId));
            }
        }

        protected void ButtonLookup_Command(object sender, CommandEventArgs e)
        {

        }

        protected void CheckBoxCopy_CheckedChanged(object sender, EventArgs e)
        {
            if(CheckBoxCopy.Checked)
            {
               
                    TextBoxAddressLine1.Text =HiddenFielAddress.Value;
                    TextBoxAddressLine2.Text = HdnAddressLine2.Value;
                    TextBoxAddressLine3.Text = HdnAddressLine3.Value;
                  

                
            }
        }
        public void Success(string text)
        {

            LiteralSuccess.Text = text;

        }
        public int CheckdirectorateNameExist(string drName)
        {
            DirectorateBiz directorateBiz = new DirectorateBiz();
            return directorateBiz.CheckDirectorateNameExist(drName);
        }
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            
                 Directorate  directorate = new Directorate();
                 DirectorateBiz  directorateBiz = new DirectorateBiz();
                 int checkname = CheckdirectorateNameExist(TextBoxDirectorateName.Text);
                    if(checkname>0)
                    {
                        LabelNameExists.Text = "DirectorateName does exist ! Please new DirectorateName !";

                       return;
                    }
                directorate.DirectorateName = TextBoxDirectorateName.Text;
                directorate.DirectorateShortDescription = TextBoxShortDescription.Text;
                directorate.ContactId = Convert.ToInt32(hdnContactId.Value);
                directorate.AddressLine1 = TextBoxAddressLine1.Text;
                directorate.AddressLine2 = TextBoxAddressLine2.Text;
                directorate.AddressLine3 = TextBoxAddressLine3.Text;
                directorate.AddressId = Convert.ToInt32(hdnAddressId.Value);
                directorate.CityTown = TextBoxCityTown.Text;
                directorate.CountryId = Convert.ToInt32(DropDownListNationCounty.SelectedValue);
                directorate.BusinessTypeId = Convert.ToInt32(hdnBussId.Value);
                directorate.DirectorateFullDescription = TextBoxFullDescription.Text;
                directorate.PhoneNumber = TextBoxPhoneNumber.Text;
                directorate.Email = TextBoxEmail.Text;
                directorate.CompanyNumber = TextBoxCompanyNumber.Text;
                directorate.CharityNumber = TextBoxCharityNumber.Text;
                directorate.WebAddress = TextBoxWebAddress.Text;
                directorate.Fax = TextBoxFax.Text;
                directorate.County = TextBoxCounty.Text;
                if (int.TryParse(Request.Params["organisationId"], out OrgId))
                {
                    directorate.OrgId = OrgId;
                }
            bool result = directorateBiz.AddDirectorate(directorate);
            
            if (result)
            {
                PanelSuccess.Visible = true;
                Success("successfully");
                TextBoxDirectorateName.Text=string.Empty;
                TextBoxShortDescription.Text=string.Empty;
                TextBoxAddressLine1.Text=string.Empty;
                TextBoxAddressLine2.Text=string.Empty;
                TextBoxAddressLine3.Text=string.Empty;
                TextBoxCityTown.Text=string.Empty;
                TextBoxFullDescription.Text=string.Empty;
                TextBoxPhoneNumber.Text=string.Empty;
                TextBoxEmail.Text=string.Empty;
                TextBoxCompanyNumber.Text=string.Empty;
                TextBoxCharityNumber.Text=string.Empty;
                TextBoxWebAddress.Text=string.Empty;
                TextBoxFax.Text=string.Empty;
                TextBoxCounty.Text=string.Empty;
                LabelNameExists.Text = string.Empty;
                if (Session.Contents["contact"] != null)
                {
                    Session["contact"] = null;
                }
                if (Session.Contents["Address"] != null)
                {
                    Session["Address"] = null;
                }
                if(Session.Contents["Bussname"] != null)
                {
                    Session["Bussname"] = null;
                }
                if(  Session.Contents["Sic"] != null)
                {
                    Session["Sic"] = null;
                }
            }
        }
       
    }
}